﻿#region

using System;
using System.ComponentModel;
using System.Linq;
using System.Web.Mvc;
using InfoTech.VinhSon.Bussiness;
using InfoTech.VinhSon.Models;
using VinhSon.Models;

#endregion

namespace VinhSon.Controllers
{
    public class PageCategoryController : BasePageController
    {
        //
        // GET: /PageCategory/

        public ActionResult Index(int id)
        {
            Category category = UnitOfWork.CategoryRepository.Select(c => c.Id == id)
                                          .FirstOrDefault();
            if(category != null)
            {
                int status = category.Status ?? 0;
                if((status & CategoryModel.SHOW_AS_PHOTO_GALERRY) == CategoryModel.SHOW_AS_PHOTO_GALERRY)
                {
                    return View("PhotoGalerry",
                                category);
                }
                if((status & CategoryModel.SHOW_AS_VIDEO_GALERRY) == CategoryModel.SHOW_AS_VIDEO_GALERRY)
                {
                    return View("VideoGalerry",
                                category);
                }
                return View(category);
            }
            return View("Notfound");
        }

        public ActionResult PhotoGalerry(int id)
        {
            Category category = UnitOfWork.CategoryRepository.Select(c => c.Id == id)
                                          .FirstOrDefault();
            if(category != null)
                return View(category);
            return View("Notfound");
        }

        public ActionResult VideoGalerry(int id)
        {
            Category category = UnitOfWork.CategoryRepository.Select(c => c.Id == id)
                                          .FirstOrDefault();
            if(category != null)
                return View(category);
            return View("Notfound");
        }

        public ActionResult Article(int id,
                                    string page)
        {
            int temp;
            int.TryParse(page,
                         out temp);
            if(temp <= 0)
                temp = 1;
            return PartialView(UnitOfWork.ArticleRepository.Select(c => c.Category.Id == id && c.Activated == true && (c.Inactive == null || c.Inactive == false),
                                                                   c => c.LastModified,
                                                                   ListSortDirection.Descending)
                                         .Skip((temp - 1) * Constant.NUMBER_ARTICLE_PER_PAGE)
                                         .Take(Constant.NUMBER_ARTICLE_PER_PAGE));
        }

        public ActionResult PhotoArticle(int id,
                                         string page)
        {
            int temp;
            int.TryParse(page,
                         out temp);
            if(temp <= 0)
                temp = 1;
            return PartialView(UnitOfWork.ArticleRepository.Select(c => c.Category.Id == id && c.Activated == true && (c.Inactive == null || c.Inactive == false),
                                                                   c => c.LastModified,
                                                                   ListSortDirection.Descending)
                                         .Skip((temp - 1) * Constant.NUMBER_PHOTO_ARTICLE_PER_PAGE)
                                         .Take(Constant.NUMBER_PHOTO_ARTICLE_PER_PAGE));
        }

        public ActionResult VideoArticle(int id,
                                         string page)
        {
            int temp;
            int.TryParse(page,
                         out temp);
            if(temp <= 0)
                temp = 1;
            return PartialView(UnitOfWork.CSectionRepository.Select(c => c.Category.Id == id && c.Type == CSection.VIDEO && c.Activated == true && (c.Inactive == null || c.Inactive == false),
                                                                    c => c.LastModified,
                                                                    ListSortDirection.Descending)
                                         .Skip((temp - 1) * Constant.NUMBER_PHOTO_ARTICLE_PER_PAGE)
                                         .Take(Constant.NUMBER_PHOTO_ARTICLE_PER_PAGE));
        }

        public ActionResult PagingArticle(Category category,
                                          string page)
        {
            int temp;
            int.TryParse(page,
                         out temp);
            if(temp <= 0)
                temp = 1;
            ViewBag.Page = temp;
            ViewBag.Count = UnitOfWork.ArticleRepository.Select(c => c.Category.Id == category.Id && c.Inactive == false && c.Activated == true)
                                      .Count();
            return PartialView(category);
        }

        public ActionResult PagingPhotoArticle(Category category,
                                               string page)
        {
            int temp;
            int.TryParse(page,
                         out temp);
            if(temp <= 0)
                temp = 1;
            ViewBag.Page = temp;
            ViewBag.Count = UnitOfWork.ArticleRepository.Select(c => c.Category.Id == category.Id && c.Inactive == false && c.Activated == true)
                                      .Count();
            return PartialView(category);
        }

        public ActionResult PagingVideoArticle(Category category,
                                               string page)
        {
            int temp;
            int.TryParse(page,
                         out temp);
            if(temp <= 0)
                temp = 1;
            ViewBag.Page = temp;
            ViewBag.Count = UnitOfWork.CSectionRepository.Select(c => c.Category.Id == category.Id && c.Type == CSection.VIDEO && c.Inactive == false && c.Activated == true)
                                      .Count();
            return PartialView(category);
        }

        public ActionResult RightColumn()
        {
            return PartialView(UnitOfWork.GroupRepository.Select(c => (c.FromDate == null || c.FromDate <= DateTime.Now) && (c.ToDate == null || c.ToDate >= DateTime.Now) && c.CategoryPage == true && c.Activated == true && (c.Inactive == false || c.Inactive == null),
                                                                 "Layout"));
        }
    }
}
